package com.api.oa.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import com.api.oa.model.entity.OaAttend;
import com.api.oa.services.IOaAttendService;
import com.api.shopapi.controller.BaseController;
import com.api.shopapi.entity.MyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("/oa/attend")
public class OaAttendController extends BaseController {

    @Autowired
    private IOaAttendService iOaAttendService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public MyResult Add(HttpServletRequest req, @RequestBody OaAttend org) {
        try {
            String remoteAddr = req.getRemoteAddr();
            org.setAttendsIp(remoteAddr);
            org.setAttendsTime(new Date());
            org.setWeekOfDay(DateUtil.date().dayOfWeekEnum().toChinese());
            return iOaAttendService.add(org) == 1 ? success(org) : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "/today/user/android", method = RequestMethod.GET)
    public MyResult listTodayUserAndroid(@RequestParam(required = false) String uid,@RequestParam(required = false) Integer pageIndex,@RequestParam(required = false) Integer pageSize) {
        try {
            return success(iOaAttendService.listByUserToday(uid,pageIndex,pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }
    /**
     * 今日考勤
     * @param key
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/today/user", method = RequestMethod.GET)
    public MyResult listTodayUser(@RequestParam(required = false) String key,@RequestParam(required = false) String govCode,@RequestParam(required = false) Integer pageIndex,@RequestParam(required = false) Integer pageSize) {
        try {
            return success(iOaAttendService.listByToday(key,govCode,pageIndex,pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    //周统计
    @RequestMapping(value = "/week/user", method = RequestMethod.GET)
    public MyResult listWeekUser(@RequestParam(defaultValue = "0") Integer index,@RequestParam(required = false) String uid,@RequestParam(required = false) Integer pageIndex,@RequestParam(required = false) Integer pageSize) {
        try {
            DateTime dateTime = DateUtil.offsetWeek(new Date(), index);
            DateTime startTime = DateUtil.beginOfWeek(dateTime);
            DateTime endTime = DateUtil.endOfWeek(dateTime);
            return success(iOaAttendService.listByWeek(startTime.toDateStr(),endTime.toDateStr(),uid,pageIndex,pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/remove/{id}", method = RequestMethod.GET)
    public MyResult remove(@PathVariable Long id) {
        try {
            iOaAttendService.remove(id);
            return success("删除成功!");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public MyResult update(@RequestBody OaAttend org) {
        try {
            return iOaAttendService.update(org) == 1 ?  success("操作成功") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MyResult list(@RequestParam(required = false) String name,@RequestParam(required = false) Integer pageIndex,@RequestParam(required = false) Integer pageSize) {
        try {
            return success(iOaAttendService.list(pageIndex,pageSize,name));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/one/{id}", method = RequestMethod.GET)
    public MyResult one(@PathVariable Long id) {
        try {
            return success(id == null ? null :iOaAttendService.one(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


}
